package com.Fate.servlet;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.Fate.common.CodeMsg;
import com.Fate.common.Constant;
import com.Fate.common.Result;
import com.Fate.pojo.User;
import com.Fate.service.IUserServiece;
import com.Fate.service.impl.UserServiceImpl;
import com.Fate.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user.do")
public class UserServlet extends HttpServlet {
 IUserServiece userServiece = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String tag = req.getParameter("service");
        try {
            Method m = this.getClass().getDeclaredMethod(tag, HttpServletRequest.class, HttpServletResponse.class);
            m.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 处理用户登录请求
     */
    protected void login(HttpServletRequest req,HttpServletResponse resp){
        String checkCode = req.getParameter("checkCode");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //校验验证码
        HttpSession session = req.getSession();
        //获取session中的验证码
        Object o = session.getAttribute(Constant.CHECK_CODE_KEY);
        //校验验证码
        if (o==null || !o.toString().equals(checkCode)){
            Result rs = new Result(CodeMsg.USER_CHECK_CODE_ERROR);
            RespUtil.write(resp,rs);
            return;
        }
        //调用登录的业务方法
        Result rs = userServiece.login(username, password);
        if (rs.getCode().equals(CodeMsg.SUCCESS.code)){
            session.setAttribute(Constant.CURRENT_USER_KEY,rs.getData());
            rs.setData(null);
        }
        //向浏览器返回业务消息
        RespUtil.write(resp,rs);
    }

    /**
     * 处理 用户列表请求
     */
    protected void page(HttpServletRequest req,HttpServletResponse resp){
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        Integer pageNo = 1;
        Integer pageSize = 10;
        //进行数据的校验
        if(NumberUtil.isNumber(page)){
            pageNo = Integer.parseInt(page)>0?Integer.parseInt(page):pageNo;
        }
        if(NumberUtil.isNumber(limit)){
            pageSize = Integer.parseInt(limit)>0?Integer.parseInt(limit):pageSize;
        }
        String username = req.getParameter("username");
        String realname = req.getParameter("realname");
        String deleted = req.getParameter("deleted");
        String role = req.getParameter("role");
        //存储所有查询参数的容器
        Map<String, String> params = new HashMap<>();
        if (StrUtil.isNotBlank(username)){
            params.put("username",username);
        }
        if (StrUtil.isNotBlank(realname)){
            params.put("realname",realname);
        }
        if (StrUtil.isNotBlank(deleted)){
            params.put("deleted",deleted);
        }
         if (StrUtil.isNotBlank(role)){
            params.put("role",role);
        }
        Result rs= userServiece.queryPage(params, pageNo, pageSize);
        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 处理 处理重置密码的请求
     */
    protected void resetPwd(HttpServletRequest req,HttpServletResponse resp){
        String id = req.getParameter("id");
        Result rs =userServiece.resetPassword(id);


        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 处理 处理重置密码的请求
     */
    protected void deleted(HttpServletRequest req,HttpServletResponse resp){
        String id = req.getParameter("id");
        Result rs =userServiece.del(id);
        //将数据输出
        RespUtil.write(resp,rs);
    }
    /**
     * 处理 处理新建用户的请求
     */
    protected void add(HttpServletRequest req,HttpServletResponse resp){
        String username = req.getParameter("username");
        String realname = req.getParameter("realname");
        Integer role = Integer.valueOf(req.getParameter("role"));
        Result rs =userServiece.add(username,realname,role);
        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 退出
     */
    protected void loginout(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //删除session
        //跳转到登陆页面
       req.getSession().invalidate();
       resp.sendRedirect("index.jsp");
    }

    /**
     * 获取所有的业务员
     */
    protected void salesmans(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        Result rs =userServiece.queryAllsalesman();
        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 修改员工密码
     */
    protected void modifyPassword(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String nowpassword = req.getParameter("nowpassword");
        String password = req.getParameter("password");
        //获取登录的用户
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER_KEY);
        String getpassword = user.getPassword();
        //判断原密码是否正确
        if (password.equals(getpassword)){
            //插入数据
            Result rs = userServiece.modify(nowpassword,user.getId());
            //将数据输出
            RespUtil.write(resp,rs);
            return;
        }else {
            Result rs = new Result(CodeMsg.USER_NAME_PASSWORD_ERROR);
            RespUtil.write(resp,rs);
        }
    }
}




















